package com.eqxiu.ieditor.weixin.service;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.eqxiu.ieditor.weixin.api.MessageServiceApi;
import com.eqxiu.ieditor.weixin.api.WeiXinServiceApi;
import com.eqxiu.ieditor.weixin.common.properties.WxProperties;
import com.eqxiu.ieditor.weixin.dto.AccessToken;
import com.eqxiu.ieditor.weixin.dto.TemplateMessage;
import com.eqxiu.ieditor.weixin.util.HttpUtils;

@Component
public class MessageServiceApiImpl implements MessageServiceApi {

    @Autowired
    private WxProperties properties;

    @Autowired
    private WeiXinServiceApi weixinServiceApi;

    private Logger logger = LoggerFactory.getLogger(getClass().getName());

    @Override
    public void sendTextMsg(String touser, String msgType, String content) {

    }

    @Override
    public void sendVoiceMsg(String touser, String msgType, String media_id) {

    }

    @Override
    public void sendNewsMsg(String touser, String msgType, String articles) {

    }

    @Override
    public void sendTemplateMsg(String appId, TemplateMessage message) {

        AccessToken tokenOpt = weixinServiceApi.getAccessToken(appId);
        logger.info("access token is {}", tokenOpt.getAccessToken());
        String url = String.format(properties.getSendTemplateUrl(), tokenOpt.getAccessToken());
        logger.info("url is {}", url);

        try {
            String content = HttpUtils.post(url, JSON.toJSONString(message), HttpUtils.CT_JSON);
            logger.info("sendTemplateMsg response : {}", content);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
